/**
 * @Author Ligg
 * @Time 2025/10/14
 **/
package com.ligg.apiclient.controller;

import com.ligg.common.enums.OrderStatus;
import com.ligg.common.module.entity.OrderEntity;
import com.ligg.common.module.vo.OrderVo;
import com.ligg.common.module.vo.PageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.annotation.JsonView;
import com.ligg.common.enums.BusinessStates;
import com.ligg.common.module.dto.OrderDto;
import com.ligg.common.module.dto.PayDto;
import com.ligg.common.module.vo.OrderInfoVo;
import com.ligg.common.module.vo.Views;
import com.ligg.common.utils.Response;
import com.ligg.order.service.OrderService;

import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

/**
 * 订单控接口
 */
@Tag(name = "订单接口")
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/client/user/order")
public class ClientOrderController {

    private final OrderService orderService;

    /**
     * 创建订单
     */
    @Operation(summary = "创建订单")
    @PostMapping
    public Response<String> createOrder(@Validated @RequestBody OrderDto orderDto) {
        String orderNo = orderService.createOrder(orderDto);
        return Response.success(BusinessStates.SUCCESS, orderNo);
    }

    /**
     * 获取订单信息
     *
     * @param orderNo 订单号
     */
    @JsonView(Views.SimpleView.class)
    @Operation(summary = "获取订单信息")
    @GetMapping("/info")
    public Response<OrderInfoVo> getOrderInfo(@NotNull String orderNo) {
        Long expireTime = orderService.getOrderExpireTime(orderNo);

        OrderInfoVo orderInfo = orderService.getOrderInfo(orderNo);
        // 检查查询结果是否为空
        if (orderInfo == null) {
            return Response.error(BusinessStates.NOT_FOUND, "订单不存在");
        }

        orderInfo.setExpireTime(expireTime);
        return Response.success(BusinessStates.SUCCESS, orderInfo);
    }

    /**
     * 支付订单
     */
    @Operation(summary = "支付订单")
    @PostMapping("/pay")
    public Response<String> payOrder(@RequestBody @Validated PayDto pay) {
        orderService.payOrder(pay);
        return Response.success(BusinessStates.SUCCESS);
    }

    /**
     * 获取用户订单列表
     */
    @Operation(summary = "获取用户订单列表")
    @GetMapping("/list")
    public Response<PageVo<OrderVo>> getUserOrderList(@NotNull Long pageNum,
                                                      @NotNull Long pageSize,
                                                      @RequestParam(required = false) OrderStatus status,
                                                      @RequestParam(required = false) String keyword) {
        PageVo<OrderVo> userOrderList = orderService.getUserOrderList(pageNum, pageSize,status,keyword);
        return Response.success(BusinessStates.SUCCESS, userOrderList);
    }

    /**
     * 取消订单
     */
    @PatchMapping("/{order_no}")
    @Operation(summary = "取消订单")
    public Response<String> cancelOrder(@NotNull @PathVariable String order_no) {
        OrderEntity order = orderService.getOrderByOderNo(order_no);
        if (order == null) {
            return Response.error(BusinessStates.NOT_FOUND, "订单不存在");
        }

        if (order.getStatus() != OrderStatus.UNPAID) {
            return Response.error(BusinessStates.VALIDATION_FAILED, "订单已支付");
        }
        orderService.cancelOrder(order.getId());
        return Response.success(BusinessStates.SUCCESS);
    }
}
